package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StreamPushMapper {

    @Insert("INSERT INTO stream_push (app, stream, total_reader_count, origin_type, origin_type_str, " +
            "create_stamp, alive_second) VALUES" +
            "('${app}', '${stream}', '${totalReaderCount}', '${originType}', '${originTypeStr}', " +
            "'${createStamp}', '${aliveSecond}' )")
    int add(StreamPushItem streamPushItem);

    @Update("UPDATE stream_push " +
            "SET app=#{app}," +
            "stream=#{stream}," +
            "total_reader_count=#{totalReaderCount}, " +
            "origin_type=#{originType}," +
            "origin_type_str=#{originTypeStr}, " +
            "create_stamp=#{createStamp}, " +
            "alive_second=#{aliveSecond} " +
            "WHERE app=#{app} AND stream=#{stream}")
    int update(StreamPushItem streamPushItem);

    @Delete("DELETE FROM stream_push WHERE app=#{app} AND stream=#{stream}")
    int del(String app, String stream);

    @Select("SELECT st.*, pgs.gb_id, pgs.status, pgs.name, pgs.longitude, pgs.latitude FROM stream_push st LEFT JOIN gb_stream pgs on st.app = pgs.app AND st.stream = pgs.stream")
    @ResultMap("streamPush")
    List<StreamPushItem> selectAll();

    @Select("SELECT st.*, pgs.gb_id, pgs.status, pgs.name, pgs.longitude, pgs.latitude FROM stream_push st LEFT JOIN gb_stream pgs on st.app = pgs.app AND st.stream = pgs.stream WHERE st.enable=${enable}")
    @ResultMap("streamPush")
    List<StreamPushItem> selectForEnable(boolean enable);

    @Select("SELECT st.*, pgs.gb_id, pgs.status, pgs.name, pgs.longitude, pgs.latitude FROM stream_push st LEFT JOIN gb_stream pgs on st.app = pgs.app AND st.stream = pgs.stream WHERE st.app=#{app} AND st.stream=#{stream}")
    @Results(id="streamPush",
    value = { 
       @Result(property = "stream", column = "stream"),@Result(property = "app", column = "app"), 
       @Result(property = "totalReaderCount", column = "total_reader_count"), @Result(property = "originType", column = "origin_type"), 
       @Result(property = "originTypeStr", column = "origin_type_str"), @Result(property = "createStamp", column = "create_stamp"), 
       @Result(property = "aliveSecond", column = "alive_second"), 
       @Result(property = "gbId", column = "gb_id"), 
       @Result(property = "status", column = "status"), @Result(property = "name", column = "name"),
       @Result(property = "longitude", column = "longitude"), @Result(property = "latitude", column = "latitude"),
       })
    StreamPushItem selectOne(String app, String stream);

    @Insert("<script>"  +
            "INSERT INTO stream_push (app, stream, total_reader_count, origin_type, origin_type_str, " +
            "create_stamp, alive_second) " +
            "VALUES <foreach collection='streamPushItems' item='item' index='index' >" +
            "( '${item.app}', '${item.stream}', '${item.totalReaderCount}', '${item.originType}', " +
            "'${item.originTypeStr}','${item.createStamp}', '${item.aliveSecond}' )" +
            " </foreach>" +
            "</script>")
    void addAll(List<StreamPushItem> streamPushItems);

    @Delete("DELETE FROM stream_push")
    void clear();

}
